package org.kl.bf.service.basic;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.kl.bf.entity.basic.Calendar;
import org.kl.bf.entity.basic.CalendarType;
import org.kl.bf.repository.basic.CalendarDao;
import org.kl.bf.repository.basic.CalendarTypeDao;
import org.kl.bf.utils.CRUDUtils;
import org.kl.bf.utils.KLDynamicSpecifications;
import org.kl.bf.utils.KLSearchFilter;
import org.kl.bf.web.base.BasicController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.utils.Collections3;

/**
 * @Author zl
 * @CreateTime 2014-7-16 下午14:31:30
 */
// Spring Bean的标识.
@Component
// 默认将类中的所有public函数纳入事务管理.
@Transactional
public class CalendarTypeService {

	private CalendarTypeDao calendarTypeDao;
	private CalendarDao calendarDao;

	public CalendarType getCalendarType(String guid) {
		return calendarTypeDao.find(CalendarType.class, guid);
	}

	public void saveCalendarType(CalendarType entity) {
		calendarTypeDao.save(entity);
		List<Calendar> calendarList = calendarDao.findByCalendarTypeIdAndUserId(entity.getId(), BasicController.getCurrentUserId());
		for (Calendar c : calendarList) {
			c.setBackgroundColor(entity.getBackgroundColor());
			calendarDao.save(c);
		}
	}

	public void deleteCalendarType(String guid) {
		calendarTypeDao.delete(guid);
	}

	public List<CalendarType> getAllCalendarType() {
		return (List<CalendarType>) calendarTypeDao.findAll();
	}

	public List<CalendarType> getCalendarTypes(String id) {
		return (List<CalendarType>) calendarTypeDao.findByUserId(id);
	}

	public CalendarType getCalendarTypeByUSerIdAndTypeName(String userId, String typeName) {
		return calendarTypeDao.findByUserIdAndTypeName(userId, typeName);
	}

	public Page<CalendarType> getUserCalendarType(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortType,
			String orderType) {
		PageRequest pageRequest = CRUDUtils.buildPageRequest(pageNumber, pageSize, sortType, orderType);
		Specification<CalendarType> spec = buildSpecification(searchParams);

		return calendarTypeDao.findAll(spec, pageRequest);
	}

	public String multiDelete(String ids) {
		String[] idArray = StringUtils.split(ids, ",");
		int i = 0, j = 0;
		for (String id : idArray) {
			if (StringUtils.isNotBlank(id)) {
				if (Collections3.isEmpty(calendarDao.findByCalendarTypeIdAndUserId(id, BasicController.getCurrentUserId()))) {
					calendarTypeDao.delete(id);
					i++;
				} else {
					j++;
				}
			}
		}
		return "删除" + i + "条记录,忽略" + j + "条存在日程的日程类型";
	}

	/**
	 * 创建动态查询条件组合.
	 */
	private Specification<CalendarType> buildSpecification(Map<String, Object> searchParams) {
		Map<String, KLSearchFilter> filters = KLSearchFilter.parse(searchParams);
		Specification<CalendarType> spec = KLDynamicSpecifications.byKLSearchFilter(filters.values(), CalendarType.class);
		return spec;
	}

	@Autowired
	public void setCalendarTypeDao(CalendarTypeDao calendarTypeDao) {
		this.calendarTypeDao = calendarTypeDao;
	}

	@Autowired
	public void setCalendarDao(CalendarDao calendarDao) {
		this.calendarDao = calendarDao;
	}

}
